package com.btkjgc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.ScoreRule;
import com.btkjgc.models.extend.ScoreRuleModel;
import com.btkjgc.models.extend.ScoreRuleReceiveModel;
import com.btkjgc.service.intf.IntegralManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class IntegralManageController {

	static Logger logger = Logger.getLogger(IntegralManageController.class);

    @Autowired
	private IntegralManageService_Intf integralManageService;

	@RequestMapping(value = "integral_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,ScoreRuleReceiveModel scoreRuleReceiveModel) {

		ModelAndView mv = new ModelAndView("integral_manage");
		getDataList(request, mv,scoreRuleReceiveModel);
		return mv;
	}
	
  
	 
	@SuppressWarnings("unchecked")
	private void getDataList(HttpServletRequest request, ModelAndView mv,ScoreRuleReceiveModel scoreRuleReceiveModel) {
 
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "aut.tel","like", scoreRuleReceiveModel.getTel()};
		Object[] arr2 = { "aut.account","like", scoreRuleReceiveModel.getAccount()};
		paramList.add(arr1);
		paramList.add(arr2);
		

		String sql ="SELECT\n" +
				"	srrt.*,aut.account,\n" +
				" aut.tel,aut.name accountName,\n" +
				" srt.score,srt.amount\n" +
				"FROM\n" +
				"	score_rule_receive_t srrt\n" +
				"LEFT JOIN account_user_t aut on aut.id=srrt.accountid and aut.del=0\n" +
				"LEFT JOIN score_rule_t srt on srt.id=srrt.scoreid and srt.del=0\n" +
				"WHERE\n" +
				"	srrt.del = 0";

		sql = integralManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = integralManageService.getCount(sql, paramList);

		// 获取分页查询的数据结果
		List<ScoreRuleReceiveModel> scoreRuleReceiveModelList = integralManageService.getList(
				ScoreRuleReceiveModel.class, paramList, sql, pageIndex, 15, sortfield);

		// 设置后台用户界面所需的其他数据
		integralManageService.setListData(request, mv, pageIndex, totalRecord,
				scoreRuleReceiveModelList);

	}

	
	


	@RequestMapping(value = "get_integral_rule")
	@ResponseBody
	public ScoreRule getIntegralRule(ModelAndView mv,HttpServletRequest request) {

		
		ScoreRule scoreRule=(ScoreRule) HibernateUtil.executeQueryOneObject(" from ScoreRule");
 
		return scoreRule;
	}
	
	
	@RequestMapping(value = "add_score_rule/{a}")
	@ResponseBody
	public   AjaxObj jsonAddScoreRuleModel(HttpServletRequest request,
			@PathVariable("a") int a, ScoreRuleModel scoreRuleModel) {
 
		AjaxObj ajaxObj = null;
        ajaxObj = integralManageService
					.insertScoreRuleModel(scoreRuleModel);
			
		return ajaxObj;
	}

	
	 
	@RequestMapping(value = "save_integral_rule")
	public String saveIntegralule(HttpServletRequest request,ScoreRuleModel scoreRuleModel) {

		integralManageService.saveIntegralule(scoreRuleModel);
		return "redirect:/integral_manage";
	}
	
	
	
}
